﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace MyVocationSpace
{
    public partial class EditProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] != null)
            {
                if (!IsPostBack)
                {
                    GetData(Convert.ToInt32(Session["UserID"]));

                    BAL.Industry.PopulateCountry(ref ddlCountry);
                    //BAL.Industry.PopulateIndustry(ref ddlIndustry);
                    PopulateEducation();
                    PopulateExperience();


                    if (imgUserPhoto.Src == "UsersPhoto/icon_no_photo_60x60.png")
                    {
                        lnkDeletePhoto.Visible = false;
                    }
                    else
                    {
                        lnkDeletePhoto.Visible = true;
                    }

                }
            }
            else
            {
                Response.Redirect("Home.aspx");
            }


        }

        public void GetData(int IDUser)
        {
            ViewState["IDUser"] = IDUser;
            string[] detail = BAL.Users.GetUserEditByID(IDUser);

            txtFName.Text = detail[0];
            txtLName.Text = detail[1];
            txtAddress.Text = detail[5];
            txtCity.Text = detail[6];

            if (detail[9].ToLower() == "false")
            {                
                    rdGender.SelectedValue = "0";               
            }
            else
            {               
                    rdGender.SelectedValue = "1";               
            }

            //txtMobile.Text = detail[10];
            TextBox1.Text = detail[11];
            txtOccup.Text = detail[12];
            // txtEducation.Text = detail[13];
            //txtCurPosition.Text = detail[14];
            //txtOrg.Text = detail[15];

            ddlCountry.SelectedValue = detail[16];
            //ddlIndustry.SelectedValue = detail[18];

            string img = detail[18];
            if (img != "")
            {
                imgUserPhoto.Src = "UsersPhoto/" + img;                
            }
            else
            {
                imgUserPhoto.Src = "UsersPhoto/icon_no_photo_60x60.png";                
            }

            imgUserPhoto.Visible = true;


            //ListItem lstCont = ddlCountry.Items.FindByValue(detail[16]);
            //if (lstCont != null)
            //{
            //    ddlCountry.SelectedValue = lstCont.Value;
            //}

            //ListItem lstIndus = ddlIndustry.Items.FindByValue(detail[18]);
            //if (lstIndus != null)
            //{
            //    ddlIndustry.SelectedValue = lstIndus.Value;
            //}          


        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            PopulateEducation();
        }

        protected void btnSubmitExp_Click(object sender, EventArgs e)
        {
            PopulateExperience();
        }


        protected void ImageSize(object sender,ServerValidateEventArgs  e)
        {
            if (fuPic.HasFile)
            {
                if (fuPic.PostedFile.ContentLength > 20960)
                {
                    e.IsValid = false;
                }
                else
                {
                    e.IsValid = true;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int IDUser = Convert.ToInt32(ViewState["IDUser"]);
            string fName = txtFName.Text;
            string lName = txtLName.Text;
            string add = txtAddress.Text;
            string city = txtCity.Text;
            //string currPos = txtCurPosition.Text;
            //string mob = txtMobile.Text;
            // string edu = txtEducation.Text;
            //string edu2 = txtEducation2.Text;
            //string edu3 = txtEducation3.Text;

           // string org = txtOrg.Text;
            string occup = txtOccup.Text;
            string zip = TextBox1.Text;
            bool gender = false;

            if (rdGender.SelectedValue=="1")
            {
                gender = true;
            }

            string pic = fuPic.FileName;

            if (pic == "")
            {
                if (imgUserPhoto.Src != "")
                {
                    pic = imgUserPhoto.Src.Substring(imgUserPhoto.Src.IndexOf('/') + 1);
                }
                else
                {
                    pic = "icon_no_photo_60x60.png";
                }
            }
            else
            {
                pic = fuPic.FileName;
            }




            int count = Convert.ToInt32(ddlCountry.SelectedValue);
            //int indus = Convert.ToInt32(ddlIndustry.SelectedValue);
            //string workexp = ddlWorkExperience.SelectedValue;


            BAL.Users.UpdateUserProfile(fName, lName, gender, add, zip, count, occup, city, pic, IDUser);
            //BAL.Users.UpdateExperienceDetails(currPos2, org2, indus2, workexp2, currPos3, org3, indus3, workexp3, IDUser);
            if (fuPic.FileName != "")
            {
                fuPic.SaveAs(Server.MapPath("UsersPhoto/" + fuPic.FileName));
            }

            Response.Redirect("MyProfile.aspx?Updated=1");
        }

        public void PopulateEducation()
        {
            DataSet ds = BAL.Users.GetAllUserCourseByUserID(Convert.ToInt32(Session["UserID"]));
            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView;

            if (pgds.DataSource != null)
            {
                dlUserEducation.DataSource = pgds.DataSource;
                dlUserEducation.DataBind();
            }
        }

        protected void dlUserEducation_ItemDataBound(object sender, DataListItemEventArgs e)
        {

        }

        protected void lnkEducationEdit_Click(object sender, EventArgs e)
        {
            LinkButton lnkEducation = (LinkButton)sender;
            DataListItem dlItem = (DataListItem)lnkEducation.Parent;
            int itemIndex = dlItem.ItemIndex;
            lblIDUserCourse.Text = dlItem.ItemIndex.ToString();
            int IDUserCourse = Convert.ToInt32(dlUserEducation.DataKeys[itemIndex]);
            string[] detail = BAL.Users.GetUserCourseByUserCourseID(IDUserCourse);
            txtCourseUpdate.Text = detail[0];
            txtCollegeUpdate.Text = detail[1];
            txtYearUpdate.Text = detail[2];

            ModalPopupExtender_UserEducation.Show();

        }

        protected void btnEducationUpdate_Click(object sender, EventArgs e)
        {
            int itemIndex = Convert.ToInt32(lblIDUserCourse.Text);
            int IDUserCourse = Convert.ToInt32(dlUserEducation.DataKeys[itemIndex]);

            string txtcrs = txtCourseUpdate.Text;
            string txtClg = txtCollegeUpdate.Text;
            string year = txtYearUpdate.Text;

            BAL.Users.UpdateUserCourse(IDUserCourse, txtcrs, txtClg, year);

            PopulateEducation();

        }

        public void PopulateExperience()
        {
            DataSet ds = BAL.Users.GetAllUserExperienceByUserID(Convert.ToInt32(Session["UserID"]));
            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView;

            if (pgds.DataSource != null)
            {
                dlProfessionalExperience.DataSource = pgds.DataSource;
                dlProfessionalExperience.DataBind();

            }
        }

        protected void lnkExperienceEdit_Click(object sender, EventArgs e)
        {
            LinkButton lnkExpEdit = (LinkButton)sender;
            DataListItem dl = (DataListItem)lnkExpEdit.Parent;
            int itemIndex = Convert.ToInt32(dl.ItemIndex);
            lblIDUserExperience.Text = dl.ItemIndex.ToString();
            int IDUserExperience = Convert.ToInt32(dlProfessionalExperience.DataKeys[itemIndex]);

            string[] detail = BAL.Users.GetUserExperienceByUserExperienceID(IDUserExperience);

            txtDesignationUpdate.Text = detail[0];
            txtCompanyUpdate.Text = detail[1];
            txtLocationUpdate.Text = detail[2];
            txtExpYearFromUpdate.Text = detail[3];
            txtExpYearToUpdate.Text = detail[4];

            ModalPopupExtender_UserExperience.Show();

        }

        protected void dlProfessionalExperience_ItemDataBound(object sender, DataListItemEventArgs e)
        {
           

        }

        protected void btnExperienceUpdate_Click(object sender, EventArgs e)
        { 
            int itemIndex = Convert.ToInt32(lblIDUserExperience.Text);
            int IDUserExperience = Convert.ToInt32(dlProfessionalExperience.DataKeys[itemIndex]);

            string desig = txtDesignationUpdate.Text;
            string comp = txtCompanyUpdate.Text;
            string loc = txtLocationUpdate.Text;
            string yearFrom = txtExpYearFromUpdate.Text;
            string yearTo = txtExpYearToUpdate.Text;

            BAL.Users.UpdateUserExperience(IDUserExperience, desig, comp, loc, yearFrom, yearTo);

            PopulateExperience();
        }

        protected void lnkEducationDelete_Click(object sender, EventArgs e)
        {
            LinkButton lnk=(LinkButton)sender;
            DataListItem dlItem=(DataListItem)lnk.Parent;
            int itemIndex=Convert.ToInt32(dlItem.ItemIndex);
            int IDCourse = Convert.ToInt32(dlUserEducation.DataKeys[itemIndex]);
            BAL.Users.DeleteUserCourse(IDCourse);
            PopulateEducation();
        }

        protected void lnkExperienceDelete_Click(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            DataListItem dlItem = (DataListItem)lnk.Parent;
            int itemIndex = Convert.ToInt32(dlItem.ItemIndex);
            int IDExp = Convert.ToInt32(dlProfessionalExperience.DataKeys[itemIndex]);

            BAL.Users.DeleteUserExperience(IDExp);
            PopulateExperience();

        }

        protected void lnkDeletePhoto_Click(object sender, EventArgs e)
        {
            string photo = "icon_no_photo_60x60.png";              

            BAL.Users.UpdateUserPhoto(Convert.ToInt32(Session["UserID"]), photo);
            GetData(Convert.ToInt32(Session["UserID"]));

            lnkDeletePhoto.Visible = false;
        }       

    }
}